#
#   __init__.py.py
#   journalismManger
#
#   Created by luoxh on 2019-07-23-18:20.
#   Copyright © 2019 luoxh. All rights reserved.
#
from datetime import datetime

from mongoengine import *
from werkzeug.security import check_password_hash, generate_password_hash

from src.model import BaseDocument


class User(BaseDocument):
    username = StringField(min_length=4, max_length=20, unique=True, required=True)
    _password = StringField(unique=True, required=True)
    birthday = DateTimeField(default=datetime.now)
    avatar = StringField()
    is_lock = BooleanField(default=False)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)

    def check_password(self, raw_password):
        return check_password_hash(self.password, raw_password)
